import 'package:icooker/configs/api_configs.dart';
import 'package:icooker/configs/app_configs.dart';
import 'package:icooker/converts/json/base_bean_entry.dart';
import 'package:icooker/model/meaildetail/MealDetailData.dart';
import 'package:icooker/model/reommend_data.dart';
import 'package:icooker/pages/details/meail_detail_v1.dart';
import 'package:icooker/repository/base_model.dart';
import 'package:icooker/repository/http_service.dart';

/// 远程仓库
class RemoteRepository {
  static RemoteRepository _instance = RemoteRepository._internal();

  factory RemoteRepository() => _instance;

  RemoteRepository._internal();

  static RemoteRepository getInstance() => _instance;

  Future<List<RecommendData>> featchRecommends() async {
    BaseModel result = await HttpService.getInstance()
        .get(ApiConfigs.INDEX_HOME_RECOMMEND_URL);
    print("RemoteRepository result： $result");

    List<RecommendData> dataList = [];
    try {
      if (result != null && result.data != null && result.data is List) {
        var list = result.data as List;
        list.forEach((element) {
          /// json转换使用该工具类:https://javiercbk.github.io/json_to_dart/
          /// 不建议使用，转换后的数据需要大量的自己修复。添加空安全
          var reInfo = RecommendData.fromJson(element);
          print("reInfo: $reInfo");
          dataList.add(reInfo);
        });
      }
    } catch (e) {
      print(e);
    }
    return dataList;
  }

  Future<List<RecommendData>> featchRecommends2() async {
    var result = await HttpService.getInstance()
        .getWithEntry(ApiConfigs.INDEX_HOME_RECOMMEND_URL);
    print("RemoteRepository result： $result");

    List<RecommendData> dataList = [];
    try {
      if (result != null) {
        var list = result.getList<RecommendData>();
        if (list != null) {
          dataList.addAll(list);
        }
      }
    } catch (e) {
      print(e);
    }
    return dataList;
  }

  Future<List<RecommendData?>?> featchRecommends3() async {
    var list = await HttpService.getInstance()
        .getWithEntrys<RecommendData>(ApiConfigs.INDEX_HOME_RECOMMEND_URL);
    return list;
  }

  Future<MealDetailData?> featchMealDetails(args) async {
    // post请求，数据在查询参数中
    var list = await HttpService.getInstance()
        .postWithEntry<MealDetailData>(ApiConfigs.RECIPE_DETAIL_URL,params: args);
    return list;
  }
}
